package cn.com.connext.scrm.service;

import cn.com.connext.scrm.domain.master.PrizeRate;
import cn.com.connext.scrm.repository.master.PrizeRateRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 2 * @Author: shawn
 * 3 * @Date: 2020/8/11 4:18 下午
 * 4
 */
@Service
public class PrizeRateService {

    private final Logger log = LoggerFactory.getLogger(PrizeRateService.class);

    @Autowired
    private PrizeRateRepository prizeRateRepository;

    public Integer getMaxNum(List<Integer> enableGoodIds) {
        Integer maxNum = prizeRateRepository.getMaxNum(enableGoodIds);
        return maxNum;
    }

    public Integer getGoodId(Integer randomNum) {
        Integer goodId = prizeRateRepository.getGoodId(randomNum);
        return goodId;
    }

    public List<PrizeRate> getByGoodId(List<Integer> enableGoodIds) {
        List<PrizeRate> list = prizeRateRepository.getByGoodId(enableGoodIds);
        return list;
    }

    public List<PrizeRate> getEnableGood(List<Integer> enableGoodIds) {
        List<PrizeRate> list = prizeRateRepository.getEnableGood(enableGoodIds);
        return list;
    }


    public PrizeRate editRate(Integer goodId, Integer num) {
        log.info("入参：{}，{}",goodId,num);
        PrizeRate prizeRate = prizeRateRepository.findByGoodId(goodId);
        if (null != prizeRate) {
            prizeRate.setNum(num);
            return prizeRateRepository.saveAndFlush(prizeRate);
        }
        prizeRate = new PrizeRate();
        prizeRate.setGoodId(goodId);
        prizeRate.setNum(num);
        return prizeRateRepository.saveAndFlush(prizeRate);
    }

    public Page<PrizeRate> getList(Integer page, Integer size) {
        Pageable pageable = new PageRequest(page, size);
        Page<PrizeRate> ratePage = prizeRateRepository.findAll(pageable);
        return ratePage;
    }

    public void delete(Integer id) {
        prizeRateRepository.delete(id);
    }
}
